//
//  TYZSimpleAlertView.m
//  MetalEquipUnion
//
//  Created by tangbin on 2021/8/15.
//

#import "TYZSimpleAlertView.h"
#import "UIWindow+SimpleAlertView.h"

@interface TYZSimpleAlertView ()
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) CALayer *horizontalLine; // 水平线
@property (nonatomic, strong) CALayer *verticalLine; // 垂直线

@property (nonatomic, copy) NSString *btnCancelTitle;
@property (nonatomic, strong) UIButton *btnCancel;

@property (nonatomic, copy) NSString *btnSubmitTitle;
@property (nonatomic, strong) UIButton *btnSubmit;
@property (nonatomic, strong) UIView *bgView;
 
@property (nonatomic, copy) void(^completionBlock)(void);
@property (nonatomic, copy) void(^failedBlock)(void);

/// 标题
- (void)initWithTitleLabel;

/// 垂直线
- (void)initWithHorizontalLine;

/// 水平线
- (void)initWithVerticalLine;

/// 取消
- (void)initWithBtnCancel;

/// 确定
- (void)initWithBtnSubmit;

@end

@implementation TYZSimpleAlertView

- (instancetype)initWithFrame:(CGRect)frame andTitle:(nullable NSString *)title btnCancelTitle:(nullable NSString *)btnCancelTitle btnSubmitTitle:(nullable NSString *)btnSubmitTitle
{
    self.title = title;
    self.btnCancelTitle = btnCancelTitle;
    self.btnSubmitTitle = btnSubmitTitle;
    
    return [self initWithFrame:frame];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    /// 标题
    [self initWithTitleLabel];

    /// 垂直线
    [self initWithHorizontalLine];

    /// 水平线
    [self initWithVerticalLine];

    /// 取消
    [self initWithBtnCancel];

    /// 确定
    [self initWithBtnSubmit];
}

- (void)setTextColor:(UIColor *)textColor
{
    self.titleLabel.textColor = textColor;
}

/// 标题
- (void)initWithTitleLabel
{
    float height = [self.title heightForFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] width:self.width - 30];
    height = (height < 20 ? 20 : height);
    CGRect frame = CGRectMake(15, 19, self.width - 30, height);
    _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
        label.textColor = kThemeColor;
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
    }];
    _titleLabel.text = self.title;
}

/// 垂直线
- (void)initWithHorizontalLine
{
    CGRect frame = CGRectMake(0, self.titleLabel.bottom + 15, self.width, 1);
    _horizontalLine = [CALayer drawLine:self frame:frame lineColor:kLineColor];
}

/// 水平线
- (void)initWithVerticalLine
{
    CGRect frame = CGRectMake((self.width - 1) * 0.5, self.horizontalLine.bottom, 1, self.height - self.horizontalLine.bottom);
    _verticalLine = [CALayer drawLine:self frame:frame lineColor:kLineColor];
}

/// 取消
- (void)initWithBtnCancel
{
    @weakify(self);
    CGRect frame = CGRectMake(0, _horizontalLine.bottom, self.verticalLine.left, self.height - self.horizontalLine.bottom);
    _btnCancel = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] action:@selector(clickedCancel:) propertiesBlock:^(UIButton * _Nonnull button) {
        @strongify(self);
        [button setTitle:self.btnCancelTitle forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#424242"] forState:UIControlStateNormal];
    }];
}

/// 确定
- (void)initWithBtnSubmit
{
    @weakify(self);
    CGRect frame = _btnCancel.frame;
    frame.origin.x = self.verticalLine.right;
    _btnSubmit = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] action:@selector(clickedSubmit:) propertiesBlock:^(UIButton * _Nonnull button) {
        @strongify(self);
        [button setTitle:self.btnSubmitTitle forState:UIControlStateNormal];
        [button setTitleColor:kThemeColor forState:UIControlStateNormal];
    }];
}

- (void)setTitleMsg:(NSString *)titleMsg
{
    _titleMsg = titleMsg;
    _titleLabel.text = titleMsg;
}


- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock();
    }
}

- (void)showWithCompletion:(void (^)(void))completionBlock failed:(void (^)(void))failedBlock
{
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.simpleAlertView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.simpleAlertMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.simpleAlertView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
//        [maskView addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
            self.alpha = 1;
        }];
    }
}

- (void)hide
{
    if (kCSKeyWindow.simpleAlertView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.simpleAlertMaskView.alpha = 0;
        } completion:^(BOOL finished) {
            [kCSKeyWindow.simpleAlertView removeFromSuperview];
            [kCSKeyWindow.simpleAlertMaskView removeFromSuperview];
            kCSKeyWindow.simpleAlertMaskView = nil;
            kCSKeyWindow.simpleAlertView = nil;
        }];
    }
}

/// 得到视图的高度
/// @param title title description
+ (CGFloat)getAlertViewHeight:(NSString *)title
{
    float height = [title heightForFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium] width:kTYZSimpleAlertViewWidth - 30];
    height = (height < 20 ? 20 : height);
    
    return kTYZSimpleAlertViewMinHeight - 20 + height;
}


@end
